package com.atguigu.yygh.hosp.hosp.controller;


import com.atguigu.yygh.common.result.R;

import com.atguigu.yygh.handler.YyghException;
import com.atguigu.yygh.hosp.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-10-30
 */
@Api(description = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospital-set")
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    @ApiOperation(value = "医院设置列表")
    @GetMapping("/findAll")
    public R findAll(){

        try {
            int a = 10/0;
        }catch(Exception e) {

            throw new YyghException(20001,"出现自定义异常");
        }
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list",list);
    }

    @ApiOperation(value = "医院设置delete")
    @DeleteMapping("/{id}")
    public R removeById(@ApiParam(name = "id", value = "jiangshiid", required = true) @PathVariable String id) {
        //todo
        hospitalSetService.removeById(id);

        return R.ok();
    }


    @ApiOperation(value = "医院设置page")
    @GetMapping("/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<HospitalSet> pageParam = new Page<>(page,limit);

        hospitalSetService.page(pageParam);

        List<HospitalSet> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }



    @ApiOperation(value = "医院设置条件查询")
    @PostMapping("/findPageCondition/{page}/{limit}")
    public R queryPage(
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit,
            @ApiParam(name = "hospitalSetQueryVo",value = "查询对象",required = false)
            @RequestBody(required = false) HospitalQueryVo hospitalQueryVo
            ){


        Page<HospitalSet> hospitalSetPage = new Page<>(page,limit);

        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

        if (hospitalQueryVo==null){
            hospitalSetService.page(hospitalSetPage,queryWrapper);
        }else {
            String hosname = hospitalQueryVo.getHosname();
            String hoscode = hospitalQueryVo.getHoscode();

            if (!StringUtils.isEmpty(hosname)){
                queryWrapper.like("hosname",hosname);

            }

            if (!StringUtils.isEmpty(hoscode)){
                queryWrapper.eq("hoscode",hoscode);
            }

            hospitalSetService.page(hospitalSetPage,queryWrapper);

        }



        List<HospitalSet> records = hospitalSetPage.getRecords();

        long total = hospitalSetPage.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "新增医院设置")
    @PostMapping("/saveHospSet")
    public R save(
            @ApiParam(name = "hospitalSet",value = "医院设置对象", required = true)
            @RequestBody HospitalSet hospitalSet){


        hospitalSetService.save(hospitalSet);
        return R.ok();
    }


    @ApiOperation(value = "根据ID查询医院设置")
    @GetMapping("/getHospSet/{id}")
    public R getById(
            @ApiParam(name = "id",value = "医院设置ID",required = true)
            @PathVariable String id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item",hospitalSet);

    }




    @ApiOperation(value = "根据ID修改医院设置")
    @PostMapping("updateHospSet")
    public R updateById(@ApiParam(name = "hospitalSet", value = "医院设置对象", required = true)
                        @RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }


    @ApiOperation(value = "根据ID批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public R batchRemove(
            @ApiParam(name = "idList",value = "id集合",required = true)
            @RequestBody List<Long> idList
    ){

        hospitalSetService.removeByIds(idList);

        return R.ok();
    }


    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

}

